package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        System.out.println(" 新增图书！ ");
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入图书名： ");
        String name = scanner.nextLine();

        System.out.println("请输入作者名： ");
        String author = scanner.nextLine();

        System.out.println("请输入图书类型： ");
        String type = scanner.nextLine();

        System.out.println("请输入图书价格： ");
        int price = scanner.nextInt();


        Book book = new Book(name,author,price,type);

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            //  将已存在的书 遍历赋值给 existBook
            Book existBook = bookList.getBook(i);
            if(existBook.getName().equals(name)){
                System.out.println("已经存在这本书了，不能在存储了");
                return;
            }
        }

        bookList.setBook(book);
        // 修改usedSize
        bookList.setUsedSize(currentSize+1);  // 29行 定义的currentSize
    }
}
